package com.innovation.chengzhangriji;

import android.content.Context;
import android.content.res.Configuration;

import com.innovation.chengzhangriji.http.BaseRequest;
import com.innovation.chengzhangriji.http.CheckVersionRequest;
import com.innovation.chengzhangriji.util.FileUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;

public class ConfigurationManager {

    public static final String APPLICATION_ID = "APPLICATION_ID";
    public static final String MIPUSH_APP_ID = "MIPUSH_APP_ID";
    public static final String MIPUSH_APP_KEY = "MIPUSH_APP_KEY";
    public static final String PASSWORD_SALT = "PASSWORD_SALT";
    public static final String ROOT_URL = "ROOT_URL";
    public static final String SHARE_PATH = "SHARE_PATH";
    public static final String UPDATE_URL = "UPDATE_URL";
    public static final String WX_ID = "WX_ID";

    private static volatile ConfigurationManager sInstance;

    private JSONObject mConfig = new JSONObject();

    private ConfigurationManager() { }

    public static ConfigurationManager getInstance() {
        if(sInstance == null) {
            synchronized (ConfigurationManager.class) {
                if(sInstance == null) {
                    sInstance = new ConfigurationManager();
                }
            }
        }
        return sInstance;
    }

    // 加载 Config
    public void loadConfig(Context context) {
        try {
            File configFile = Config.getAppConfig(context);
            if(!configFile.exists()) {
                InputStream in = context.getAssets().open(Config.CONFIG_FILE_NAME);
                byte[] buffer = new byte[in.available()];
                in.read(buffer, 0, buffer.length);
                in.close();
                // 复制文件到 config 目录
                FileOutputStream out = new FileOutputStream(configFile, false);
                out.write(buffer, 0, buffer.length);
                out.close();
                String configText = new String(buffer, 0, buffer.length, "UTF-8");
                mConfig = new JSONObject(configText).getJSONObject("config");
            } else {
                FileInputStream in = new FileInputStream(configFile);
                byte[] buffer = new byte[in.available()];
                in.read(buffer, 0, buffer.length);
                in.close();
                String configText = new String(buffer, 0, buffer.length, "UTF-8");
                mConfig = new JSONObject(configText).getJSONObject("config");
            }
            ((ChZhRJApplication)context.getApplicationContext()).initApi();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取配置文件的值
     * @param key
     * @return
     */
    public String get(String key) {
        return mConfig.optString(key);
    }
}
